/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package HealthMonitoringView;

import DataMining.Clustering;
import DataMining.ClusteringImpl;
import DataSource.DataEntry;
import DataSource.DataGenerator;
import DataSource.Subscriber;
import RuleInterence.RuleChecker;
import RuleInterence.RuleCheckerImpl;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Tuan
 */
public class MonitoringMIDlet extends MIDlet implements CommandListener, MIDletNavigation {

    private boolean midletPaused = false;
//<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private java.util.Hashtable __previousDisplayables = new java.util.Hashtable();
    private Command exitCommand;
    private Command itemCommand;
    private Command exitCommand1;
    private Command backCommand;
    private Command exitCommand2;
    private Command backCommand1;
    private Command okCommand;
    private Command backCommand2;
    private Form form;
    private Gauge gauge1;
    private Gauge gauge;
    private List list;
//</editor-fold>//GEN-END:|fields|0|
    private MonitoringForm frmMonitoring;
    private DataEntryForm frmDataEntry;
    private ClusteringForm frmClustering;
    private DataGenerator dataGenerator;

    /**
     * The HelloMIDlet constructor.
     */
    public MonitoringMIDlet() {
        
    }

//<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    /**
     * Switches a display to previous displayable of the current displayable.
     * The
     * <code>display</code> instance is obtain from the
     * <code>getDisplay</code> method.
     */
    private void switchToPreviousDisplayable() {
        Displayable __currentDisplayable = getDisplay().getCurrent();
        if (__currentDisplayable != null) {
            Displayable __nextDisplayable = (Displayable) __previousDisplayables.get(__currentDisplayable);
            if (__nextDisplayable != null) {
                switchDisplayable(null, __nextDisplayable);
            }
        }
    }
//</editor-fold>//GEN-END:|methods|0|
//<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize

    /**
     * Initializes the application. It is called only once when the MIDlet is
     * started. The method is called before the
     * <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        
        frmDataEntry = new DataEntryForm(this);
    }//GEN-BEGIN:|0-initialize|2|
//</editor-fold>//GEN-END:|0-initialize|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getList());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
    }//GEN-BEGIN:|3-startMIDlet|2|
//</editor-fold>//GEN-END:|3-startMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
//</editor-fold>//GEN-END:|4-resumeMIDlet|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The
     * <code>display</code> instance is taken from
     * <code>getDisplay</code> method. This method is used by all actions in the
     * design for switching displayable.
     *
     * @param alert the Alert which is temporarily set to the display;
     * if <code>null</code>, then <code>nextDisplayable</code> is set
     * immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        Displayable __currentDisplayable = display.getCurrent();
        if (__currentDisplayable != null && nextDisplayable != null) {
            __previousDisplayables.put(nextDisplayable, __currentDisplayable);
        }
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
//</editor-fold>//GEN-END:|5-switchDisplayable|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a
     * particular displayable.
     *
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == list) {//GEN-BEGIN:|7-commandAction|1|30-preAction
            if (command == List.SELECT_COMMAND) {//GEN-END:|7-commandAction|1|30-preAction
                // write pre-action user code here
                listAction();//GEN-LINE:|7-commandAction|2|30-postAction
                // write post-action user code here
            } else if (command == backCommand2) {//GEN-LINE:|7-commandAction|3|70-preAction
                // write pre-action user code here
                switchToPreviousDisplayable();//GEN-LINE:|7-commandAction|4|70-postAction
                // write post-action user code here
            } else if (command == exitCommand1) {//GEN-LINE:|7-commandAction|5|33-preAction
                // write pre-action user code here
                exitMIDlet();//GEN-LINE:|7-commandAction|6|33-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
//</editor-fold>//GEN-END:|7-commandAction|8|


//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand ">//GEN-BEGIN:|18-getter|0|18-preInit
    /**
     * Returns an initialized instance of exitCommand component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {//GEN-END:|18-getter|0|18-preInit
            // write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|18-getter|1|18-postInit
            // write post-init user code here
        }//GEN-BEGIN:|18-getter|2|
        return exitCommand;
    }
//</editor-fold>//GEN-END:|18-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initialized instance of itemCommand component.
     *
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return itemCommand;
    }
//</editor-fold>//GEN-END:|22-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: list ">//GEN-BEGIN:|28-getter|0|28-preInit
    /**
     * Returns an initialized instance of list component.
     *
     * @return the initialized component instance
     */
    public List getList() {
        if (list == null) {//GEN-END:|28-getter|0|28-preInit
            // write pre-init user code here
            list = new List("Health Monitoring App", Choice.IMPLICIT);//GEN-BEGIN:|28-getter|1|28-postInit
            list.append("Enter your data", null);
            list.append("Monitoring", null);
            list.append("Clustering", null);
            list.addCommand(getExitCommand1());
            list.addCommand(getBackCommand2());
            list.setCommandListener(this);
            list.setSelectedFlags(new boolean[]{false, false, false});//GEN-END:|28-getter|1|28-postInit
            // write post-init user code here
            
        }//GEN-BEGIN:|28-getter|2|
        return list;
    }
//</editor-fold>//GEN-END:|28-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Method: listAction ">//GEN-BEGIN:|28-action|0|28-preAction
    /**
     * Performs an action assigned to the selected list element in the list
     * component.
     */
    public void listAction() {//GEN-END:|28-action|0|28-preAction
        // enter pre-action user code here
        String __selectedString = getList().getString(getList().getSelectedIndex());//GEN-BEGIN:|28-action|1|35-preAction
        if (__selectedString != null) {
            if (__selectedString.equals("Enter your data")) {//GEN-END:|28-action|1|35-preAction
                // write pre-action user code here
                switchDisplayable(null, frmDataEntry);
//GEN-LINE:|28-action|2|35-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Monitoring")) {//GEN-LINE:|28-action|3|36-preAction
                // write pre-action user code here
                if(this.frmMonitoring != null){
                    switchDisplayable(null, this.frmMonitoring);
                } else{
                    Alert alert = new Alert("Alert", "You haven't enter the data. Please enter the data first", null, AlertType.WARNING);
                    getDisplay().setCurrent(alert);
                }
//GEN-LINE:|28-action|4|36-postAction
                // write post-action user code here
            } else if (__selectedString.equals("Clustering")) {//GEN-LINE:|28-action|5|37-preAction
                // write pre-action user code here
                if(this.frmClustering != null){
                    showClusteringForm(true);                    
                } else{
                    Alert alert = new Alert("Alert", "You haven't enter the data. Please enter the data first", null, AlertType.WARNING);
                    getDisplay().setCurrent(alert);
                }
//GEN-LINE:|28-action|6|37-postAction
                // write post-action user code here
            }//GEN-BEGIN:|28-action|7|28-postAction
        }//GEN-END:|28-action|7|28-postAction
        // enter post-action user code here
    }//GEN-BEGIN:|28-action|8|
//</editor-fold>//GEN-END:|28-action|8|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand1 ">//GEN-BEGIN:|32-getter|0|32-preInit
    /**
     * Returns an initialized instance of exitCommand1 component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand1() {
        if (exitCommand1 == null) {//GEN-END:|32-getter|0|32-preInit
            // write pre-init user code here
            exitCommand1 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|32-getter|1|32-postInit
            // write post-init user code here
        }//GEN-BEGIN:|32-getter|2|
        return exitCommand1;
    }
//</editor-fold>//GEN-END:|32-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand ">//GEN-BEGIN:|39-getter|0|39-preInit
    /**
     * Returns an initialized instance of backCommand component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand() {
        if (backCommand == null) {//GEN-END:|39-getter|0|39-preInit
            // write pre-init user code here
            backCommand = new Command("Back", Command.BACK, 0);//GEN-LINE:|39-getter|1|39-postInit
            // write post-init user code here
        }//GEN-BEGIN:|39-getter|2|
        return backCommand;
    }
//</editor-fold>//GEN-END:|39-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: exitCommand2 ">//GEN-BEGIN:|41-getter|0|41-preInit
    /**
     * Returns an initialized instance of exitCommand2 component.
     *
     * @return the initialized component instance
     */
    public Command getExitCommand2() {
        if (exitCommand2 == null) {//GEN-END:|41-getter|0|41-preInit
            // write pre-init user code here
            exitCommand2 = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|41-getter|1|41-postInit
            // write post-init user code here
        }//GEN-BEGIN:|41-getter|2|
        return exitCommand2;
    }
//</editor-fold>//GEN-END:|41-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|60-getter|0|60-preInit
    /**
     * Returns an initialized instance of okCommand component.
     *
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|60-getter|0|60-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|60-getter|1|60-postInit
            // write post-init user code here
        }//GEN-BEGIN:|60-getter|2|
        return okCommand;
    }
//</editor-fold>//GEN-END:|60-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand1 ">//GEN-BEGIN:|63-getter|0|63-preInit
    /**
     * Returns an initialized instance of backCommand1 component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand1() {
        if (backCommand1 == null) {//GEN-END:|63-getter|0|63-preInit
            // write pre-init user code here
            backCommand1 = new Command("Back", Command.BACK, 0);//GEN-LINE:|63-getter|1|63-postInit
            // write post-init user code here
        }//GEN-BEGIN:|63-getter|2|
        return backCommand1;
    }
//</editor-fold>//GEN-END:|63-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: form ">//GEN-BEGIN:|66-getter|0|66-preInit
    /**
     * Returns an initialized instance of form component.
     *
     * @return the initialized component instance
     */
    public Form getForm() {
        if (form == null) {//GEN-END:|66-getter|0|66-preInit
            // write pre-init user code here
            form = new Form("form", new Item[]{getGauge(), getGauge1()});//GEN-LINE:|66-getter|1|66-postInit
            // write post-init user code here
        }//GEN-BEGIN:|66-getter|2|
        return form;
    }
//</editor-fold>//GEN-END:|66-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: gauge ">//GEN-BEGIN:|67-getter|0|67-preInit
    /**
     * Returns an initialized instance of gauge component.
     *
     * @return the initialized component instance
     */
    public Gauge getGauge() {
        if (gauge == null) {//GEN-END:|67-getter|0|67-preInit
            // write pre-init user code here
            gauge = new Gauge("gauge", false, 100, 50);//GEN-LINE:|67-getter|1|67-postInit
            // write post-init user code here
        }//GEN-BEGIN:|67-getter|2|
        return gauge;
    }
//</editor-fold>//GEN-END:|67-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: gauge1 ">//GEN-BEGIN:|68-getter|0|68-preInit
    /**
     * Returns an initialized instance of gauge1 component.
     *
     * @return the initialized component instance
     */
    public Gauge getGauge1() {
        if (gauge1 == null) {//GEN-END:|68-getter|0|68-preInit
            // write pre-init user code here
            gauge1 = new Gauge("gauge1", false, 100, 50);//GEN-LINE:|68-getter|1|68-postInit
            // write post-init user code here
        }//GEN-BEGIN:|68-getter|2|
        return gauge1;
    }
//</editor-fold>//GEN-END:|68-getter|2|

//<editor-fold defaultstate="collapsed" desc=" Generated Getter: backCommand2 ">//GEN-BEGIN:|69-getter|0|69-preInit
    /**
     * Returns an initialized instance of backCommand2 component.
     *
     * @return the initialized component instance
     */
    public Command getBackCommand2() {
        if (backCommand2 == null) {//GEN-END:|69-getter|0|69-preInit
            // write pre-init user code here
            backCommand2 = new Command("Back", Command.BACK, 0);//GEN-LINE:|69-getter|1|69-postInit
            // write post-init user code here
        }//GEN-BEGIN:|69-getter|2|
        return backCommand2;
    }
//</editor-fold>//GEN-END:|69-getter|2|

    /**
     * Returns a display instance.
     *
     * @return the display instance.
     */
    public Display getDisplay() {
        return Display.getDisplay(this);

    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        if(this.dataGenerator != null){
            this.dataGenerator.stop();
        }        
        switchDisplayable(null, null);
        destroyApp(true);
        notifyDestroyed();        
    }

    /**
     * Called when MIDlet is started. Checks whether the MIDlet have been
     * already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet();
        } else {
            initialize();
            startMIDlet();
        }
        midletPaused = false;        
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
        if(dataGenerator != null){
            dataGenerator.stop();
        }
    }

    /**
     * Called to signal the MIDlet to terminate.
     *
     * @param unconditional if true, then the MIDlet has to be unconditionally
     * terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
        if(dataGenerator != null){
            dataGenerator.stop();
        }
    }
    
    public void goBack(){
        switchToPreviousDisplayable();
    }

    public void showMonitoringForm() {        
        switchDisplayable(null, frmMonitoring);
    }

    public void showClusteringForm(boolean startClustering) {
        switchDisplayable(null, frmClustering);        
        if(startClustering){
            this.frmClustering.start();
        }
    }

    public void dataEntered(DataEntry data) {
        if(dataGenerator != null){
            dataGenerator.stop();
        }
        dataGenerator = new DataGenerator(data);
        RuleCheckerImpl ruleChecker = new RuleCheckerImpl(data);
        if(this.frmMonitoring == null){
            this.frmMonitoring = new MonitoringForm(this, ruleChecker);
        } else{
            this.frmMonitoring.setRuleChecker(ruleChecker);
        }
        Clustering clustering = new ClusteringImpl();
        if(this.frmClustering == null){                  
            this.frmClustering = new ClusteringForm(this, clustering);
        } else {
            this.frmClustering.setClustering(clustering);
        }
        
        
        this.dataGenerator.addSubscriber(ruleChecker);
        this.dataGenerator.addSubscriber((Subscriber)clustering);
        
        this.dataGenerator.start();
        
        showMonitoringForm();
    }

    public void showMain() {
        switchDisplayable(null, list);
    }

    public void showScreen(Screen screen) {
        switchDisplayable(null, screen);
    }
    
    public void exit() {
        this.exitMIDlet();
    }
    public void showEntryDataForm(){
        switchDisplayable(null, this.frmDataEntry);
    }
}
